# import subprocess
#
# # 启动TensorBoard命令
# command = ['tensorboard', '--logdir=C:\\Users\\14798\\docker\\test\\runs']
#
# # 使用subprocess模块运行命令
# process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#
# # 读取命令输出
# output, error = process.communicate()
#
# # 打印输出结果
# print(output)
# print(error)
import docker

from ai.utils.config import docker_dataset_path, docker_volume_path

client = docker.from_env()

dataset = docker_dataset_path + "/1"  # 数据集路径
command_train = 'python classify/train.py' + \
                ' --model weights/yolov5s-cls.pt' + \
                ' --data ' + dataset + \
                ' --epochs 10' + \
                ' --batch-size 128' + \
                ' --project runs/classify/train' + \
                ' --img 224' + ' --workers 0'
print(command_train)
# 创建docker容器
container = client.containers.run('ainew:latest',
                                  name="test",
                                  volumes={
                                      docker_volume_path: {'bind': '/usr/src/app', 'mode': 'rw'}
                                  },
                                  ports={"6006": "6006"},
                                  detach=True, remove=True, tty=True,
                                  command=command_train)
# 启动TensorBoard命令
command_tensorboard = ['tensorboard', '--logdir=runs/classify/train']

# 在容器内部执行TensorBoard命令
process_tensorboard = container.exec_run(cmd=command_tensorboard, detach=True)
# 打印容器ID
print(container.id)